﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeGraphicalControl;
using FeatureModelCore.Configuration;
using Collections.TreeCollections;

namespace FeatureModelTool.ConfigGraphic
{
    public class ConfigTreeView : TreeViewControl
    {
        public ConfigTreeView()
            : base()
        {
        }

        /// Generate Tree
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="item"></param>
        protected override void generateTree(TreeObject obj, TreeViewItemControl item
            , TreeViewItemControl[] orgItems)
        {
            if (obj is ConfigRelationship)
            {
                INonLeaf nonLeaf = obj as INonLeaf;
                if (nonLeaf == null)
                {
                    return;
                }
                foreach (INonRoot nonRoot in nonLeaf.Children)
                {
                    TreeObject child = nonRoot as TreeObject;
                    if (child == null)
                    {
                        continue;
                    }

                    TreeViewItemControl newItem = _menuGenerator.GenerateTreeItem(child, orgItems);
                    newItem.Focusable = false;
                    item.Items.Add(newItem);
                    generateTree(child, newItem, orgItems);
                }
            }
            else if (obj is ConfigRoot
                || obj is ConfigNode)
            {
                INonLeaf nonLeaf = obj as INonLeaf;
                if (nonLeaf == null)
                {
                    return;
                }
                foreach (INonRoot nonRoot in nonLeaf.Children)
                {
                    TreeObject child = nonRoot as TreeObject;
                    if (child == null)
                    {
                        continue;
                    }
                    generateTree(child, item, orgItems);
                }
            }
        }
    }
}
